import re
import json


def extract_encoding_values(html_file_path):
    """
    从HTML文件中提取input encoding和output encoding的scale和offset值
    """
    try:
        with open(html_file_path, "r", encoding="utf-8") as file:
            content = file.read()

        # 查找graphs JSON数据
        graphs_match = re.search(
            r"graphs = JSON\.parse\(\'(\[.*?\])\'\);", content, re.DOTALL
        )
        if not graphs_match:
            print("未找到graphs数据")
            return None

        graphs_json = graphs_match.group(1)
        # 解析JSON数据
        graphs_data = json.loads(graphs_json)

        result = {"input_encoding": None, "output_encoding": None}

        # 遍历网络层信息查找input encoding和output encoding
        if graphs_data and len(graphs_data) > 0 and len(graphs_data[0]) > 0:
            layers = graphs_data[0][0]  # 第一个网络图的层信息

            for layer in layers:
                # 查找包含参数的层
                if "parameters" in layer:
                    for param in layer["parameters"]:
                        # 提取input encoding信息
                        if("input encoding" in param):
                            print("找到input encoding")
                            print(param.split(":")[0])
                        if (
                            "input encoding" in param
                            and "input encoding" in param.split(":")[0]
                        ):
                            scale_match = re.search(r"scale ([0-9.-]+)", param)
                            offset_match = re.search(r"offset ([0-9.-]+)", param)

                            if scale_match and offset_match:
                                result["input_encoding"] = {
                                    "scale": scale_match.group(1),
                                    "offset": offset_match.group(1),
                                }
                                print(
                                    f"找到input encoding - Scale: {result['input_encoding']['scale']}, Offset: {result['input_encoding']['offset']}"
                                )

                        # 提取output encoding信息
                        if (
                            "output encoding" in param
                            and "output encoding" in param.split(":")[0]
                        ):
                            scale_match = re.search(r"scale ([0-9.-]+)", param)
                            offset_match = re.search(r"offset ([0-9.-]+)", param)

                            if scale_match and offset_match:
                                result["output_encoding"] = {
                                    "scale": scale_match.group(1),
                                    "offset": offset_match.group(1),
                                }
                                print(
                                    f"找到output encoding - Scale: {result['output_encoding']['scale']}, Offset: {result['output_encoding']['offset']}"
                                )

        return result

    except Exception as e:
        print(f"读取文件时出错: {e}")
        return None


# # 使用示例
# html_file_path = r"D:\data\code\client\decoder_file\backup\model\QRV2_new8_pred_quantized_prepared_qcs8550.html"
# result = extract_encoding_values(html_file_path)

# if result:
#     if result["input_encoding"]:
#         t=result['input_encoding']['offset']
#         # 将t转成float,如果是整数，转成Int
#         t = float(t)
#         t = int(t) if t.is_integer() else t
#         t=abs(t)
#         print(f"offset:{type(t)}")
#         print(f"Input Encoding - Scale: {result['input_encoding']['scale']}")
#         print(f"Input Encoding - Offset: {result['input_encoding']['offset']}")
#     else:
#         print("未找到input encoding信息")

#     if result["output_encoding"]:
#         print(f"Output Encoding - Scale: {result['output_encoding']['scale']}")
#         print(f"Output Encoding - Offset: {result['output_encoding']['offset']}")
#     else:
#         print("未找到output encoding信息")
# else:
#     print("提取失败")
